define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/schedule/index' + location.search,
                    add_url: 'xiservice/schedule/add',
                    edit_url: 'xiservice/schedule/edit',
                    del_url: 'xiservice/schedule/del',
                    multi_url: 'xiservice/schedule/multi',
                    import_url: 'xiservice/schedule/import',
                    table: 'xiservice_worker_schedule',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'date', title: __('Date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'rest_start_time', title: __('Rest_start_time')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        worker: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/schedule/worker' + location.search,
                    add_url: 'xiservice/schedule/add',
                    edit_url: 'xiservice/schedule/edit',
                    del_url: 'xiservice/schedule/del',
                    multi_url: 'xiservice/schedule/multi',
                    import_url: 'xiservice/schedule/import',
                    table: 'xiservice_worker_schedule',
                }
            });
            var table = $("#table");

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();

            function updatelist () {

                let form = $('#add-form');
                let action = form.attr('action');
                let data = form.serialize();
                let div_name = $('#timelist');
                div_name.html('');
                $.ajax({
                    url:action,
                    type : 'POST',
                    data:data,
                    dataType : 'json',
                    success:function (res) {
                        if(res.code==1){
                            let res_data =res.data;
                            let str = '';
                            let td_str = '';
                            let count_num = res_data.length;
                            let count_k = count_num;
                             for (let i=0;i<count_num;i++){
                                 var time = res_data[i]["start_time_text"];
                                 // res_data[i]["start_time"] +'-'+ res_data[i]["end_time"] ;
                                 // 判断 状态
                                 let status_text='休息';
                                 let status_class='btn-danger';
                                 if(res_data[i]["checked"]==1){
                                     status_text='工作';
                                     status_class='btn-success';
                                 }
                                 if(res_data[i]["checked"]==2){
                                     status_text='订单';
                                     status_class='btn-orange';
                                 }
                                 if(res_data[i]["checked"]==3){
                                     status_text='已过';
                                     status_class='btn-gray';
                                 }
                                 let id = res_data[i]['id'];
                                 let user_id = res_data[i]['user_id'];
                                 let date = res_data[i]['date'];
                                 td_str+='<td>' +
                                     '<p class="name">'+time+'</p>' +
                                     '<a href="javascript:;" data-id="'+id+'"  data-user="'+user_id+'"      data-date="'+date+'"     class="btn work_status '+status_class+'">'+status_text+'</a>' +
                                     '</td> ';
                                var j = i+1;
                                 if(j>0 && j%4==0 || j==count_k){
                                     str+='<tr>'+td_str+'</tr>';
                                     td_str='';
                                 }
                             }
                            div_name.append(str);
                        }else {
                            layer.msg(res.msg);
                        }
                    },
                    error:function (res) {
                        layer.msg('error');
                    }
                });
            };            

            // 修改工作状态
            $(document).on('click','.work_status',function () {
                let that = $(this);
                let id = that.data('id');
                let user_id = that.data('user');
                let date = that.data('date');
                // 三个事件
                layer.confirm(__('是否切换状态'), {
                    skin : "my-skin",
                    btn: ['工作','休息','取消']//可以无限个按钮
                    ,btn3: function(index, layero){
                        //按钮【按钮三】的回调
                         layer.close(index);
                    }
                    
                }, function(index, layero){
                    //按钮【按钮一】的回调
                    work_act(that,id,1,user_id,date);
                    layer.close(index);
                }, function(index){
                    //按钮【按钮二】的回调
                    work_act(that,id,0,user_id,date);
                    layer.close(index);
                });

            });
            // 修改
            function work_act(that,id,act,user_id,date) {
                $.ajax({
                    url:'xiservice/schedule/edit_status',
                    type : 'POST',
                    data:{id:id,act:act,user_id:user_id,date:date},
                    dataType : 'json',
                    success:function (res) {
                        if(res.code==1){
                            // 未签到
                            if(act==1){
                                that.removeClass('btn-danger');
                                that.addClass('btn-success');
                                that.html('工作');
                            }else {
                                that.removeClass('btn-success');
                                that.addClass('btn-danger');
                                that.html('休息');
                            }
                        }else{
                            layer.msg(res.msg);
                        }
                    },
                    error:function (res) {
                        layer.msg('error');
                    }
                });
            }

            $(".datetimepicker").on("dp.change", function(){ 
                 updatelist();
            }); 

        },        
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
